package com.boco.blch.analysiser.compliance.HW.data;

import com.boco.blch.analysiser.compliance.ComplianceResolved;

/**
 * HDFS存储文件本地权限最小化安全基线要求项     【 数据节点】
 * @author admin
 *
 */
public class BOCO_Hdfs_VisitCtl_StorageFileMinAuth implements ComplianceResolved {
	
	//private final static String FILE_PERMISSIONS = "-rw-------";//文件权限为600
	//private final static String FLODER_PERMISSIONS = "drwx------";//文件夹权限为700

	@Override
	public int realEchoResolved(String echoResult) {
		int result = FAILED;
		if(echoResult != null && echoResult.length() > 0 && echoResult.indexOf("DataNode-start") != -1){
			String strResult = echoResult.substring(echoResult.indexOf("DataNode-start")+"DataNode-start".length(), echoResult.indexOf("DataNode-end"));
			result = getResult(strResult);
		}
		return result;
	}
	
	/**
	 * 【数据节点】
	 * 找到配置文件 hdfs-site.xml的配置项 dfs.datanode.data.dir
	 * 的值对应的文件夹以及子文件夹的目录权限是 700，文件权限是 600
	 * @param echoResult
	 * @return
	 */
	private int getResult(String echoResult){
		int result = FAILED;
		if(echoResult != null && echoResult.indexOf("value-start") != -1){//判断文件夹及子文件夹权限
			String hdfsXml = echoResult.substring(echoResult.indexOf("value-start")+"value-start".length(), echoResult.indexOf("value-end")).trim();
//			if(hdfsXml == null || hdfsXml.length() <= 0 || hdfsXml.contains(NotFindFile) 
//					|| hdfsXml.contains(NoPermission2) || hdfsXml.contains(NoPermission3)){//目录不存在  不合规
//				result = FAILED;
//			}else{
			hdfsXml = echoResult.substring(echoResult.indexOf("dir-start")+"dir-start".length(), echoResult.indexOf("dir-end")).trim();
			if(hdfsXml == null || hdfsXml.length() <= 0){
				result = SUCCESS;
				//System.out.println("hdfsxml===:"+hdfsXml);
				/*String []dirStr = hdfsXml.split("\n");
				for (int i = 0; i < dirStr.length; i++) {
					String str = dirStr[i].trim();
					if(str != null && str.length() > 0 && (str.indexOf("-rw") != -1 || str.indexOf("drw") != -1)){
						result = FAILED;
						break;
					}else{
						result = SUCCESS;
					}
					//System.out.println(str);
					if(str != null && str.length() > 0 
							&& ((str.startsWith("-r") && str.contains(FILE_PERMISSIONS)) 
									|| (str.startsWith("dr") && str.contains(FLODER_PERMISSIONS)))){
						//System.out.println(111);
						result = SUCCESS;
					}else{
						//System.out.println(322);
						result = FAILED;
						break;
					}
				}*/ 
//				}
			}
			
		}
		return result;
	}
	
	


}
